package top.drinyeit.nongmao.presenter;

import top.drinyeit.nongmao.activity.LoginActivity;
import top.drinyeit.nongmao.app.Constant;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;
import top.drinyeit.nongmao.util.SPUtil;
import top.drinyeit.nongmao.util.Validator;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by bigHead on 2018/1/2.
 */

public class LoginPresenter {
    private LoginActivity loginView;

    public LoginPresenter(LoginActivity loginView) {
        this.loginView = loginView;
    }

    //获取验证码
    public void getCode(String phone_number) {
        if (!Validator.isMobile(phone_number)) {
            loginView.showToastMsg("请输入正确的手机号码");
            return;
        }
        loginView.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                loginView.dismissLoading();
                loginView.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                loginView.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    loginView.setTime();
                    loginView.showToastMsg("已成功发送验证码");
                } else {
                    loginView.showToastMsg(o.getMessage());
                }

            }
        });

        RetrofitMethod.getInstance().get_code(subscriber, phone_number);
    }

    //登录
    public void login(final String phone_number, String code) {
        if (phone_number.length() != 11) {
            loginView.showToastMsg("请输入正确的手机号码");
            return;
        }
        if (code.length() != 4) {
            loginView.showToastMsg("请输入正确的验证码");
            return;
        }
        loginView.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                loginView.dismissLoading();
                loginView.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                loginView.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    SPUtil.setShareString(Constant.USER_ID, o.getUser_id());
                    loginView.showToastMsg("登录成功");
                    SPUtil.setShareString(Constant.LOGIN_PHONE,phone_number);
                    loginView.finish();
                } else {
                    loginView.showToastMsg(o.getMessage());
                }
            }
        });

        RetrofitMethod.getInstance().login(subscriber, phone_number, code);
    }
}
